Allow multiple search query params
authorJesús Espino <jespinog@gmail.com>
Tue, 8 Mar 2016 08:27:09 +0000 (09:27 +0100)
committerJesús Espino <jespinog@gmail.com>
Tue, 8 Mar 2016 18:18:08 +0000 (19:18 +0100)
src/bin/search.rs
tests/test_cargo_search.rs

index 8b8c010e676072ee5f4dafbdb8dabd0e0859befa..2eae9173c9cea3a5e571f9ebf9113b952accc2c1 100644 (file)
@@ -10,14 +10,14 @@ pub struct Options {
     flag_quiet: Option<bool>,
     flag_color: Option<String>,
     flag_limit: Option<u32>,
-    arg_query: String
+    arg_query: Vec<String>
 }
 
 pub const USAGE: &'static str = "
 Search packages in crates.io
 
 Usage:
-    cargo search [options] <query>
+    cargo search [options] <query>...
     cargo search [-h | --help]
 
 Options:
@@ -40,6 +40,6 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
         ..
     } = options;
 
-    try!(ops::search(&query, config, host, cmp::min(100, limit.unwrap_or(10)) as u8));
+    try!(ops::search(&query.join("+"), config, host, cmp::min(100, limit.unwrap_or(10)) as u8));
     Ok(None)
 }
index d4c4838722dcf6e27a66b560fcb98ec82ba48bf0..24fa9a5e1cd3b8ddd87d5ca2625c5ec012dc35f9 100644 (file)
@@ -86,6 +86,52 @@ test!(simple {
 hoare (0.1.1)    Design by contract style assertions for Rust", updating = UPDATING)));
 });
 
+test!(multiple_query_params {
+    let contents = r#"{
+        "crates": [{
+            "created_at": "2014-11-16T20:17:35Z",
+            "description": "Design by contract style assertions for Rust",
+            "documentation": null,
+            "downloads": 2,
+            "homepage": null,
+            "id": "hoare",
+            "keywords": [],
+            "license": null,
+            "links": {
+                "owners": "/api/v1/crates/hoare/owners",
+                "reverse_dependencies": "/api/v1/crates/hoare/reverse_dependencies",
+                "version_downloads": "/api/v1/crates/hoare/downloads",
+                "versions": "/api/v1/crates/hoare/versions"
+            },
+            "max_version": "0.1.1",
+            "name": "hoare",
+            "repository": "https://github.com/nick29581/libhoare",
+            "updated_at": "2014-11-20T21:49:21Z",
+            "versions": null
+        }],
+        "meta": {
+            "total": 1
+        }
+    }"#;
+    let base = api_path().join("api/v1/crates");
+
+    // Older versions of curl don't peel off query parameters when looking for
+    // filenames, so just make both files.
+    //
+    // On windows, though, `?` is an invalid character, but we always build curl
+    // from source there anyway!
+    File::create(&base).unwrap().write_all(contents.as_bytes()).unwrap();
+    if !cfg!(windows) {
+        File::create(&base.with_file_name("crates?q=postgres+sql&per_page=10")).unwrap()
+             .write_all(contents.as_bytes()).unwrap();
+    }
+
+    assert_that(cargo_process("search").arg("postgres").arg("sql"),
+                execs().with_status(0).with_stdout(format!("\
+{updating} registry `[..]`
+hoare (0.1.1)    Design by contract style assertions for Rust", updating = UPDATING)));
+});
+
 test!(help {
     assert_that(cargo_process("search").arg("-h"),
                 execs().with_status(0));